﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Presenters.CollectControl.FineCollectControl;
//using University.Workshop.Presenters.CollectControl.FineCollectControl;

namespace University.Workshop.Presentation.CollectControl
{
    /// <summary>
    /// Interaction logic for FineCollectControlRegister.xaml
    /// </summary>
    public partial class FineCollectControlRegister : Page, IRegisterFineCollectControl
    {
        PresenterRegisterFineCollectControl pRegisterOtherCollect;
        Dictionary<string, ICollection<Model.Allotment>> manzanos;
        bool buscar;

        public FineCollectControlRegister()
        {
            pRegisterOtherCollect = new PresenterRegisterFineCollectControl(this);
            manzanos = new Dictionary<string, ICollection<Model.Allotment>>();
            InitializeComponent();
            LlenarCbx();
        }

        public void LlenarCbx()
        {
            manzanos = pRegisterOtherCollect.LlenarCbx();

            foreach (KeyValuePair<string, ICollection<Model.Allotment>> kv in manzanos)
            {
                ManzanoCbx.Items.Add(kv.Key);
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.MainWindow.Title = "Registrar Multa";
        }

        private void CantCupyPasteOrCut(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Command == ApplicationCommands.Copy)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Cut)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Paste)
                e.Handled = true;
        }

        #region IRegisterOtherCollectControl Members

        public string Manzano
        {
            get
            {
                return ManzanoCbx.SelectedValue.ToString();
            }
        }

        public string Lote
        {
            get
            {
                return LoteCbx.SelectedValue.ToString();
            }
        }

        public string NombreSocio
        {
            set
            {
                if (value != String.Empty)
                    SocioLbl.Content = value;
                else
                    SocioLbl.Content = "-";
            }
        }

        public string CiSocio
        {
            set
            {
                if (value != String.Empty)
                    CiLbl.Content = value;
                else
                    CiLbl.Content = "-";
            }
        }

        public string ControlDetail
        {
            get
            {
                return DetailsTxt.Text;
            }
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            MessageBox.Show(App.Current.MainWindow, MessageValue, "Error!!!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }

        public bool NotifyConfirmationMessage(string MessageValue)
        {
            return MessageBox.Show(App.Current.MainWindow,
                MessageValue,
                "Atención!!!",
                MessageBoxButton.YesNo,
                MessageBoxImage.Information,
                MessageBoxResult.Yes,
                MessageBoxOptions.RightAlign) == MessageBoxResult.Yes;
        }

        #endregion

        private void RealizarBtn_Click(object sender, RoutedEventArgs e)
        {
            bool seProceso = false;

            seProceso = pRegisterOtherCollect.RegisterCollect();
            if (seProceso)
            {
                MessageBox.Show("La multa fue registrada exitosamente.");
                this.NavigationService.Navigate(new FineCollectControlRegister());
            }
            //else
            //   MessageBox.Show("Hola man");
        }

        private void ManzanoCbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CleanSearch();

            if (!LoteCbx.IsEnabled)
                LoteCbx.IsEnabled = true;
            else
            {
                if (LoteCbx.HasItems)
                    buscar = false;
                LoteCbx.Items.Clear();
            }

            ICollection<Model.Allotment> lotes = manzanos[ManzanoCbx.SelectedItem.ToString()];

            foreach (Model.Allotment a in lotes)
                LoteCbx.Items.Add(a.AllotmentName);

            buscar = true;
        }

        private void LoteCbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            bool encontro = false;
            CleanSearch();

            if (buscar)
                encontro = pRegisterOtherCollect.SearchAllotmentInfo();

            if (encontro)
                RealizarBtn.IsEnabled = true;
            else
                RealizarBtn.IsEnabled = false;
        }

        private void CleanSearch()
        {
            NombreSocio = string.Empty;
            CiSocio = string.Empty;
            DetailsTxt.Text = string.Empty;
        }

        #region IRegisterFineCollectControl Members


        //public int Descuento
        //{
        //    get { throw new NotImplementedException(); }
        //}

        #endregion
    }
}
